package com.cqc.jdk8.Jnio.channels;

import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileChannelTest {
    /**
     * 利用通道复制文件，适合大文件
     * 这种写法没有关闭  FileInputStream  FileOutputStream
     * 'FileInputStream' used without 'try'-with-resources statement
     */
    @Test
    public void f1() throws Exception {
        try (FileChannel sourceChannel = new FileInputStream("D:\\temp\\gitcode.com令牌.txt").getChannel();
             FileChannel targetChannel = new FileOutputStream("D:\\temp\\A\\git.txt").getChannel()
        ) {
            sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 这种写法关闭了  FileInputStream  FileOutputStream
     */
    @Test
    public void f2() throws Exception {
        try (FileInputStream fileInputStream = new FileInputStream("D:\\temp\\gitcode.com令牌.txt");
             FileChannel sourceChannel = fileInputStream.getChannel();
             FileOutputStream fileOutputStream = new FileOutputStream("D:\\temp\\A\\git.txt");
             FileChannel targetChannel = fileOutputStream.getChannel()
        ) {
            sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
